  //数组
  var classArr = [
    {School:"成都大学",Class:"安检一班",ClassTime:"2011-07-04",peopleNumber:30,major:"安全技术检测",teacherName:"佟湘玉"},
    {School:"农业大学",Class:"农业三班",ClassTime:"2012-08-14",peopleNumber:30,major:"农业种植",teacherName:"白玉堂"},
    {School:"四川师范大学",Class:"教师二班",ClassTime:"2013-05-12",peopleNumber:30,major:"学前教育",teacherName:"姬无命"},
    {School:"政法大学",Class:"政法三班",ClassTime:"2019-09-13",peopleNumber:30,major:"中国刑法",teacherName:"项少羽"},
    {School:"信息大学",Class:"安检一班",ClassTime:"2019-05-15",peopleNumber:30,major:"光学工程",teacherName:"荆天明"},
    {School:"畜牧大学",Class:"畜牧二班",ClassTime:"2019-04-12",peopleNumber:30,major:"畜牧养殖",teacherName:"安林"},
    {School:"工程大学",Class:"工程四班",ClassTime:"2019-03-24",peopleNumber:30,major:"室内设计",teacherName:"涂山红红"},
    {School:"土木大学",Class:"土木三班",ClassTime:"2019-02-07",peopleNumber:30,major:"建筑设计",teacherName:"王权富贵"},{School:"警察大学",Class:"刑警一班",ClassTime:"2019-01-14",peopleNumber:30,major:"刑警管理",teacherName:"东方月初"},
    {School:"警察大学",Class:"刑警二班",ClassTime:"2019-01-14",peopleNumber:30,major:"刑警管理",teacherName:"东方月初"},
    {School:"警察大学",Class:"刑警三班",ClassTime:"2019-01-14",peopleNumber:30,major:"刑警管理",teacherName:"东方月初"},
];
    var zhongyesu;//总页码
    var dangqianye = 1;//当前页
    var meiyexianshi = 4;//每页显示
    var j ; //存数组下标
//运行函数
tbdis(); 
displayshu();



// 默认
$(".btn-primary").css("display","none");


// 添加班级
$("#Appclass").click(function (){
    $("#xianshi").html("");
    $("#yes").css("display","none");
    $("#tianjia").css("display","inline");
    $("#xianshi").append(
        `<li> 学校名称：<input id="School"  type="text"></li>
         <li> 班级名称：<input id="Class"  type="text"></li>
         <li> 开班时间：<input id="ClassTime"  type="text"></li>        
         <li> 班级人数：<input id="peopleNumber"  type="text"></li>
         <li> 班级专业：<input id="major"  type="text"></li>
         <li> 带班老师：<input id="teacherName" type="text"></li>
        `)})

    $("#tianjia").click(function(){
       var Schoola = $("#School").val();
       var Classa = $("#Class").val();
       var ClassTimea = $("#ClassTime").val();
       var peopleNumbera = $("#peopleNumber").val();
       var majora = $("#major").val();
       var teacherNamea = $("#teacherName").val();
        var obj = {School:Schoola,Class:Classa,ClassTime:ClassTimea, peopleNumber:peopleNumbera, major:majora,teacherName:teacherNamea};
        classArr.push(obj);
        tbdis();
   
        

})

// 查看
$("#tbdisplay").on("click",".cakan",function(){
    
   
    var j = Number($(this).attr('name'))
    $(".btn-primary").css("display","none");
    $("#xianshi").html("");
    
    $("#xianshi").append(
        `<li> 学校：${classArr[j].School}</li>
         <li> 班级：${classArr[j].Class}</li>
         <li> 开班时间：${classArr[j].ClassTime}</li>        
         <li> 班级人数：${classArr[j].peopleNumber}</li>
         <li> 专业：${classArr[j].major}</li>
         <li> 带班老师：${classArr[j].teacherName}</li>
     `
    )
})


// 修改
$("#tbdisplay").on("click",".xiugai",function(){
    var b = Number($(this).prev().attr('name'));
    
    j = b;
    $("#yes").css("display","none");
    $("#tianjia").css("display","inline");
    $("#xianshi").html("");
    $("#tianjia").css("display","none");
    $("#yes").css("display","inline");
    $("#xianshi").append(
        `<li> 学校名称：<input id="School" value="${classArr[j].School}" type="text"></li>
         <li> 班级名称：<input id="Class" value="${classArr[j].Class}" type="text"></li>
         <li> 开班时间：<input id="ClassTime" value="${classArr[j].ClassTime}" type="text"></li>        
         <li> 班级人数：<input id="peopleNumber" value="${classArr[j].peopleNumber}" type="text"></li>
         <li> 班级专业：<input id="major" value="${classArr[j].major}" type="text"></li>
         <li> 带班老师：<input id="teacherName" value="${classArr[j].teacherName}" type="text"></li>
        `)});
        //修改后 数据返回数组
$("#yes").click(function(){
// console.log(j);
classArr[j].School= $("#School").val();
classArr[j].Class= $("#Class").val();
classArr[j].ClassTime= $("#ClassTime").val();
classArr[j].peopleNumber= $("#peopleNumber").val();
classArr[j].major= $("#major").val();
classArr[j].teacherName= $("#teacherName").val();
tbdis();
})

    // 显示数据
    function tbdis(){
        $("#tbdisplay").html("");
        var start =(dangqianye-1)*meiyexianshi;
        var end =start + meiyexianshi;
        for(var i=start ;i<end && i < classArr.length;i++ ){
            $("#tbdisplay").append(
                `<tr>                    
                    <td name="${i}">${classArr[i].School}</td>
                    <td>${classArr[i].Class}</td>     
                    <td>${classArr[i].teacherName}</td>
                    <td><span name="${i}" class="cakan" data-toggle="modal" data-target="#myModal">查看</span>   
                        <span class="xiugai" data-toggle="modal" data-target="#myModal">修改</span>
                    </td>
                </tr>`
                )
        }

    }
    // 搜索
$("#search").click(function(){
    var sousuo =$("#sousuo").val();
    
    //console.log(sousuo);
    $("#tbdisplay").html("");
for(var i = 0 ; i < classArr.length ; i++ ){
    //判断输入的值是否等于 数组里其中一个元素  注：需要输入完整的元素 如：学校 ： 成都大学  ；要完全一样  
    if(sousuo==classArr[i].School || sousuo==classArr[i].Class || sousuo ==classArr[i].teacherName ){
       
        $("#tbdisplay").append(
                `<tr>                    
                    <td>${classArr[i].School}</td>
                    <td>${classArr[i].Class}</td>     
                    <td>${classArr[i].teacherName}</td>
                    <td><span name="${i}" data-toggle="modal" data-target="#myModal" class="cakan">查看</span> 
                        <span data-toggle="modal" data-target="#myModal" class="xiugai">修改</span>
                    </td>
                </tr>`
                )
    }else if(sousuo==""){
        //如果输入为空直接运行 显示数组  （也就是之前内容不变） 
        tbdis();
        break;
    }
  
    
}
}
    
)

// 页码显示
function displayshu(){
            var toto = classArr.length;
            zhongyesu =Math.ceil(toto/meiyexianshi);
            if(zhongyesu<dangqianye){
                dangqianye = zhongyesu;
            }
            $("#div1").html("");
            for(var i = 1 ; i <=zhongyesu; i++){
                $("#yema").append(`<button  class="ymcolor">${i}</button>`);
            }
        }


// 默认给第一页加点击效果
$("#yema").children().first().attr("id","activ")
//页码点击效果
$("#yema").on("click",".ymcolor",function(){
    $("#yema").children().removeAttr("id","activ");
    $(this).attr("id","activ");
    dangqianye =Number( $(this).text());
    tbdis();
})


  